﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Admin.Model;
@using CNKI.TPI.Web.Base;

<script>
    function addCate() {
        $("#hiddenSysID").val(0);
        $("#catename").val("");
        showCate();
    }
    function editCate(name, id) {

        $("#hiddenSysID").val(id);
        $("#catename").val(name);
        showCate();
    }
    function delCate(id) {
        var urlstr = "@Html.Raw(Url.Action("DelFav", "UserCenter"))";
        var dataobj = { "id": id };
        confirmDialog('如果确认删除该分类，则该分类下的收藏记录都会被删除!', function () {
            $.ajax({
                type: "GET",
                url: timestamp(urlstr),
                data: dataobj,
                success: function (msg) {
                    if (msg == "True") {
                        var urlstr = "@Html.Raw(Url.Action("ManagerCategoryIndex", "UserCenter"))";
                        ajaxLoad("initload", timestamp(urlstr)); //刷新列表页
                    }
                    else {
                        messageAlert("删除失败！");
                    }
                },
                error: function (e)
                { }
            })
        })
    }
    function saveCate() {
        
        var mname = $("#catename").val();
        if ($.trim(mname) == "") {
            messageAlert("名称不能为空！");
            return;
        }
        else {
            if (!regtext(mname)) {
                messageAlert("请输入名称(不允许输入特殊字符)！");
                return;
            }
        }
        //判断名称是否重复
        var m = $("#hiddenSysID").val();
        $.ajax({
            type: "GET",
            async: false,
            data: { "id": m, "cateName": mname },
            url: timestamp("@Html.Raw(Url.Action("IsExistFavName", "UserCenter"))"),
            success: function (obj) {
                if (obj == "True") {
                    messageAlert("该名称已经存在！");
                }
                else {
                    savedata();
                }

            },
            error: function (e)
            { }
        })
    }
    function savedata() {
        var m = $("#hiddenSysID").val();
        var mname = $("#catename").val();
        var saveurl = "@Html.Raw(Url.Action("SaveFav", "UserCenter"))";
        $.ajax({
            type: "POST",
            data: { "favname": mname, "favstatus": 1, "id": m },
            url: saveurl,
            success: function (f) {
                if (f == "True") {
                    closeCate();
                    ajaxLoad("initload", '@Html.Raw(Url.Action("ManagerCategoryIndex", "UserCenter"))');
                }
                else {
                    messageAlert("操作失败！");
                }
            },
            error: function (e)
            { }
        })
    }
    function closeCate() {
        $(".talkDivBg").hide();
        $(".addCate").hide();
    }
    function showCate() {
        $(".talkDivBg").show();
        $(".addCate").show();
    }

    function closeSort() {
        $(".talkDivBg").hide();
        $(".sortCate").hide();
    }
    function showSort() {
        $(".talkDivBg").show();
        $(".sortCate").show();
    }
    function sortCate() {

        ;
        if ($("#FavCount").val() > 0) {
            //获取对应的数据
            $.ajax({
                type: "GET",
                url: timestamp("@Html.Raw(Url.Action("GetFavList", "UserCenter"))"),
                beforeSend: function () { $("#loading").show(); },
                success: function (msg) {
                    var obj = eval(msg);
                    $(".ulfield").empty();
                    if (obj != null && obj.length > 0) {
                        for (var i = 0; i < obj.length; i++) {
                            var listr = " <li class=\"f-ct\" style=\"margin-left: 2em;\">  <label class=\"f-label block right\">" +
                           " <input type=\"checkbox\" class=\"f-checkboxR\"  value='" + obj[i].FavoriteCategoryID + "' />" + obj[i].CategoryName + "</label></li>";
                            $(".ulfield").append(listr);
                        }
                    }
                    $("#loading").hide();
                    showSort();
                },
                error: function (e)
                { $("#loading").hide(); }

            })

        }
        else {
            messageAlert("暂无数据，不支持排序！");
        }

    }
    function saveSort() {
        var jsondata = [];
        $('.wrapR-c-box-b-right ul input').each(function (index) {
            var dataobj = {
                "FavoriteCategoryID": $(this).val(),
                "SortNo": index
            }
            jsondata.push(dataobj);

        })
        var dataold = { "list": jsondata };
        $.ajax({
            type: "POST",
            url: "@Html.Raw(Url.Action("UpdateFavSort", "UserCenter"))",
            contentType: "application/json",
            data: JSON.stringify(dataold),
            success: function (msg) {
                if (msg == "True") {
                    closeSort();
                    ajaxLoad("initload", '@Html.Raw(Url.Action("ManagerCategoryIndex", "UserCenter"))');

                   }
                   else {
                       messageAlert("操作失败！");
                   }
               },
            error: function (e)
            { }
        })

    }

    function downHistory() {
        $('#pageIndexNow').val(1);
        searchDownData()
    }
    $(function () {
        downHistory();
    })
    function searchDownData() {
        $.ajax({
            type: "GET",
            url: timestamp("@Html.Raw(Url.Action("ManagerCategoryIndexSearch", "UserCenter"))"),
            data: { "pageIndexNow": $('#pageIndexNow').val() },
            beforeSend: function () { $("#loading").show(); },
            success: function (msg) {
                $("#catList").html("");
                $("#catList").html(msg);
                $("#loading").hide();
            },
            error: function (e)
            { $("#loading").hide(); }
        })
    }
    //点击分页
    function searchPageNew(n) {
        $('#pageIndexNow').val(n);
        searchDownData();
    }

</script>
<div class="wrapR-c" style='padding-top: 20px; padding-bottom: 20px'>
    <div class='mb20 clearfix'>
        <a onclick="addCate()" class="btn btn-m btn-submit btn-defa-f">添加</a>
        <a onclick="sortCate()" class="btn btn-m btn-submit btn-defa-f">排序</a>
    </div>
    <div id="catList">
    </div>
</div>
<input type="hidden" id="hiddenSysID" />
<input type="hidden" id="pageIndexNow" name="pageIndexNow" value="1" />
<!--透明背景-->
<div class="talkDivBg" style="display: none; z-index: 999">
    <!--talkDiv弹出层(不带图片，宽550)-->
    <div class="talkDiv talkDivTr talkW-550 form-h addCate" style='margin-top: -120px; display: none'>
        <div class="talkDiv-title">
            <span>管理收藏分类</span>
            <a class="icon icon-close" title="关闭" href="javascript:void(0);" onclick="closeCate()"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body talkDiv-body1">

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">分类名称：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span3" id="catename" maxlength="32" style="width:390px">
                    <span class="f-label-re">*</span>
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label"></label>
                </div>
                <div class="f-ct" style="margin-left: 150px">
                    <a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="saveCate()">保存</a> &#12288;
                    <a href="javascript:void(0);" class="btn btn-m btn-submit btn-defa-f" onclick="closeCate()">取消</a>
                </div>
            </div>
        </div>
    </div>

    <div class="talkDiv talkDivTr talkW-550 form-h sortCate" style='margin-top: -260px; display: none'>
        <div class="talkDiv-title">
            <span>分类排序</span>
            <a class="icon icon-close" title="关闭" href="javascript:void(0);" onclick="closeSort()"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body talkDiv-body1" style="max-height: 480px">

            <div class="f-gp" style="padding-left: 70px">

                <div class='wrapR-c-box wrapR-c-r wrapR-c-r-l' style="overflow-y: visible; height: 430px">
                    <div class="wrapR-c-box-t">分类列表</div>
                    <div class="wrapR-c-box-b-right" style="overflow-y: scroll; height: 380px">
                        <ul class="ulfield"></ul>
                    </div>
                </div>

                <div class="wrapR-c-r-r form-h posa" style="top: 80px; left: 400px;">
                    <div>
                        <a title="移至最顶部" onclick=" movefirst(this)" class="iconfont-s iconfont-upest-s"></a>
                        <br />
                        <a title="上移" onclick="moveablove(this)" class="iconfont-s iconfont-up-s"></a>
                        <br />
                        <a title="下移" onclick="movedown(this)" class="iconfont-s iconfont-down-s"></a>
                        <br />
                        <a title="移至最底部" onclick="movelast(this)" class="iconfont-s iconfont-downest-s"></a>
                    </div>


                </div>
            </div>
            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label"></label>
                </div>
                <div class="f-ct" style="margin-left: 150px">
                    <a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="saveSort()">保存</a> &#12288;
                </div>
            </div>

        </div>


    </div>
</div>